package com.warehouse.controller;

import com.warehouse.common.Result;
import com.warehouse.dto.OrderDTO;
import com.warehouse.entity.Order;
import com.warehouse.service.OrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.List;

@RestController
@RequestMapping("/api/orders")
@CrossOrigin(origins = "*")
public class OrderController {

    @Autowired
    private OrderService orderService;

    @GetMapping
    public Result<List<Order>> getAllOrders() {
        List<Order> orders = orderService.getAllOrders();
        return Result.success(orders);
    }

    @GetMapping("/with-details")
    public Result<List<OrderDTO>> getAllOrdersWithDetails() {
        List<OrderDTO> orders = orderService.getAllOrdersWithDetails();
        return Result.success(orders);
    }

    @GetMapping("/{id}")
    public Result<Order> getOrderById(@PathVariable Long id) {
        Order order = orderService.getOrderById(id);
        if (order != null) {
            return Result.success(order);
        }
        return Result.error("订单不存在");
    }

    @GetMapping("/{id}/with-details")
    public Result<OrderDTO> getOrderWithDetailsById(@PathVariable Long id) {
        OrderDTO order = orderService.getOrderWithDetailsById(id);
        if (order != null) {
            return Result.success(order);
        }
        return Result.error("订单不存在");
    }

    @GetMapping("/number/{orderNumber}")
    public Result<Order> getOrderByNumber(@PathVariable String orderNumber) {
        Order order = orderService.getOrderByOrderNumber(orderNumber);
        if (order != null) {
            return Result.success(order);
        }
        return Result.error("订单不存在");
    }

    @GetMapping("/customer")
    public Result<List<Order>> getOrdersByCustomer(@RequestParam String customer) {
        List<Order> orders = orderService.getOrdersByCustomer(customer);
        return Result.success(orders);
    }

    @GetMapping("/status/{status}")
    public Result<List<Order>> getOrdersByStatus(@PathVariable String status) {
        List<Order> orders = orderService.getOrdersByStatus(status);
        return Result.success(orders);
    }

    @GetMapping("/date-range")
    public Result<List<Order>> getOrdersByDateRange(
            @RequestParam @DateTimeFormat(iso = DateTimeFormat.ISO.DATE_TIME) LocalDateTime startTime,
            @RequestParam @DateTimeFormat(iso = DateTimeFormat.ISO.DATE_TIME) LocalDateTime endTime) {
        List<Order> orders = orderService.getOrdersByDateRange(startTime, endTime);
        return Result.success(orders);
    }

    @PostMapping
    public Result<OrderDTO> createOrder(@RequestBody OrderDTO orderDTO) {
        OrderDTO createdOrder = orderService.createOrder(orderDTO);
        return Result.success(createdOrder);
    }

    @PutMapping("/{id}")
    public Result<OrderDTO> updateOrder(@PathVariable Long id, @RequestBody OrderDTO orderDTO) {
        orderDTO.setId(id);
        OrderDTO updatedOrder = orderService.updateOrder(orderDTO);
        return Result.success(updatedOrder);
    }

    @PutMapping("/{id}/status")
    public Result<Boolean> updateOrderStatus(@PathVariable Long id, @RequestParam String status) {
        boolean success = orderService.updateOrderStatus(id, status);
        if (success) {
            return Result.success(true);
        }
        return Result.error("更新订单状态失败");
    }

    @DeleteMapping("/{id}")
    public Result<Boolean> deleteOrder(@PathVariable Long id) {
        boolean success = orderService.deleteOrder(id);
        if (success) {
            return Result.success(true);
        }
        return Result.error("删除订单失败");
    }
} 